package com.google.code.sharecontent.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.google.code.sharecontent.exception.ChangeStatusException;
import com.google.code.sharecontent.model.Content;
import com.google.code.sharecontent.model.ContentDisplay;
import com.google.code.sharecontent.model.Paging;
import com.google.code.sharecontent.model.Resource;
import com.google.code.sharecontent.model.ResourceDisplay;
import com.google.code.sharecontent.model.Status;
import com.google.code.sharecontent.model.User;

@Scope(ScopeType.CONVERSATION)
@Name("mycontents")
public class MyContentsAction extends BaseAction {

	@In("daoAction")
	private DaoAction daoAction;
	
	@In("scanApplication")
	private ScanApplication scanApplication;

	private String selectedContentStatus = "";
	private String selectedContent;
	private Integer selectedResources;
	private Boolean selectedAll;
	private Map<String, Integer> resources;
	
	private Paging paging;
	
	public Boolean getSelectedAll() {
		return selectedAll;
	}

	public void setSelectedAll(Boolean selectedAll) {
		this.selectedAll = selectedAll;
	}

	private List<ContentDisplay> contents;
	
	public void checkSelectedAll() {
		if (this.contents != null) {
			for (ContentDisplay contentDisplay : this.contents) {
				contentDisplay.setSelect(this.selectedAll);
			}
		}
	}
	
	public List<ContentDisplay> getContents() {
		if (this.contents == null) {
			factoryContents();
		}
		return contents;
	}

	private void factoryContents() {
		List<Content> list = new ArrayList<Content>();
		this.contents = new ArrayList<ContentDisplay>();
		try {
			if (this.selectedResources != null && this.selectedContentStatus != null && this.selectedContentStatus.length() > 0) {
				Status status = daoAction.getStatusDao().getByCode(this.selectedContentStatus);
				if (status != null) {
					ResultContainer<Content> resultContainer = null;
					if (this.selectedContent != null && this.selectedContent.length()>0) {
						resultContainer = this.daoAction.getContentDao().listByResourceAndStatusAndPhrase(this.paging,this.selectedResources,status.getId(),this.selectedContent); 
					} else {
						resultContainer = this.daoAction.getContentDao().listByResourceAndStatus(this.paging,this.selectedResources,status.getId());
					}
					if (resultContainer != null) {
						list.addAll(resultContainer.getList());
						this.setPaging(resultContainer.getPaging());
					}
				}
			}
			for (Content resource : list) {
				ContentDisplay resourceDisplay = ContentDisplay.createContentDisplay(resource);
				Status status;
					status = this.daoAction.getStatusDao().get(resource.getStatusId());
				resourceDisplay.setStatus(status.getName());
				this.contents.add(resourceDisplay);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
	}

	/**
	 * Проверяет статус, если он входит в следующий тип статуса для текущего
	 * @param status
	 * @return
	 */
	public Boolean isButtonNotActive(String status) {
		List<String> list = Status.nextContentStatuses.get(this.selectedContentStatus);
		if (list != null) {
			return new Boolean(!list.contains(status));
		}
		
		return Boolean.TRUE;
	}
	
	public Map<String, Integer> getResources() {
		if (resources == null) {
			resources = new HashMap<String, Integer>();
			try {
				List<Resource> list = this.daoAction.getResourceDao().list();
				if (list != null) {
					for (Resource resource : list) {
						resources.put(resource.getName(), resource.getId());
					}
				}
			} catch (SQLException e) {
				log.error(e.getMessage());
			}
		}
		return resources;
	}
	
	public void changeSelectedResources() {
		eraseValues();
		factoryContents();
	}
	
	public void changeSelectedContent() {
		eraseValues();
		factoryContents();
	}
	
	public void changeContentStatus() {
		eraseValues();
		factoryContents();
	}

	private void eraseValues() {
		this.contents = null;
		this.selectedAll = null;
		this.resources = null;
		this.getPaging().setPage(0);
	}
	
	public Map<String, String> getContentStatuses() {
		return daoAction.getStatusDao().getContentStatuses(getSelectedResources());
/*		Map<String, String> result = new HashMap<String, String>();
		result.put("","");
		for (String status : Status.statusesListFoContents) {
			result.put(status, status);
		}
		return result;*/
	}
	
	public void setContents(List<ContentDisplay> contents) {
		this.contents = contents;
	}
	
	public void toShare() {
		try {
			if (this.contents != null) {
				Status statusActive = scanApplication.getStatusActive();
				for (ContentDisplay contentDisplay : this.contents) {
					if (contentDisplay.getSelect().booleanValue()) {
						log.info("selected " + contentDisplay.getFilename());
						try {
							this.daoAction.getContentDao().changeStatus(contentDisplay.getContent(), statusActive);
						} catch (ChangeStatusException  ce) {
							//FIXME Show Error message!
							return;
						}
//						Content selectedContent = daoAction.getContentDao().get(contentDisplay.getContent().getId());
//						if (selectedContent.getStatusId().equals(scanApplication.getStatusWaitingСonfirm().getId())) {
//							Integer statusActive = scanApplication.getStatusActive().getId();
//							selectedContent.setStatusId(statusActive);
//							this.daoAction.getContentDao().save(selectedContent);
//						}
					}
				}
			}
			eraseValues();
			} catch (Exception e1) {
				log.error(e1.getMessage());
				;
			}
	}
	
	public void toProcessing() {
		try {
		if (this.contents != null) {
			Status statusToPrecess = scanApplication.getStatusToProcess();
			for (ContentDisplay contentDisplay : this.contents) {
				if (contentDisplay.getSelect().booleanValue()) {
					log.info("selected " + contentDisplay.getFilename());
					try {
						this.daoAction.getContentDao().changeStatus(contentDisplay.getContent(), statusToPrecess);
					} catch (ChangeStatusException  ce) {
						//FIXME Show Error message!
						return;
					}
					contentDisplay.setSelect(Boolean.FALSE);
//					Content selectedContent = contentDisplay.getContent();
//					selectedContent.setStatusId(statusToPrecess);
//					this.daoAction.getContentDao().save(contentDisplay.getContent());
				}
			}
		}
		eraseValues();
		} catch (Exception e1) {
			log.error(e1.getMessage());
			;
		}
	}
	
	@Begin
	public void start() {
		super.start();
		log.info(" start");
		eraseValues();
	}
	
	@End
	public void end() {
		log.info(" end");
	}

	public void setSelectedContentStatus(String selectedContentStatus) {
		this.selectedContentStatus = selectedContentStatus;
	}

	public String getSelectedContentStatus() {
		return selectedContentStatus;
	}

	public void setSelectedResources(Integer selectedResources) {
		this.selectedResources = selectedResources;
	}

	public Integer getSelectedResources() {
		if (selectedResources == null) {
			Map<String, Integer> resMap = getResources();
			if (resMap != null && resMap.size()>0) {
				selectedResources = resMap.get(resMap.keySet().toArray()[0]);
			}
		}
		return selectedResources;
	}

	public void setSelectedContent(String selectedContent) {
		this.selectedContent = selectedContent;
	}

	public String getSelectedContent() {
		return selectedContent;
	}

	public void setPaging(Paging paging) {
		this.paging = paging;
	}

	public void setPage(Integer page) {
		if (this.paging != null) {
			if (this.paging.getPage() != null && !this.paging.getPage().equals(page)) {
				this.paging.setPage(page);
				factoryContents();
			}
		}
	}

	public Integer getPage() {
		if (this.paging != null) {
			return this.paging.getPage();
		}
		return null;
	}

	public Paging getPaging() {
		if (this.paging == null) {
			this.setPaging(new Paging());
		}
		return paging;
	}

}
